package com.lh.lepay.service.impl;

import ch.qos.logback.core.util.StringUtil;
import cn.hutool.db.sql.SqlExecutor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lh.lepay.dao.BillingManagementDao;
import com.lh.lepay.entity.BillingManagement;
import com.lh.lepay.entity.vo.BillingManagementVo;
import com.lh.lepay.service.BillingManagementService;
import com.lh.lepay.utils.AmountValid;
import com.lh.lepay.utils.HttpStatus;
import com.lh.lepay.utils.ResponseData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (BillingManagement.)表服务实现类
 *
 * @author makejava
 * @since 2024-12-04 14:35:46
 */
@Slf4j
@Service
public class BillingManagementServiceImpl extends ServiceImpl<BillingManagementDao,BillingManagement> implements BillingManagementService {

    @Resource
    private BillingManagementDao billingManagementDao;

    @Resource
    private AmountValid amountValid;


    //@PreAuthorize("hasAnyAuthority('sys:info:test')")
    @Override
    public ResponseData getBillingListData(Integer currentPage, Integer pageSize,String queryString){
        // TODO 分页与查询同时使用一个业务
        Page<BillingManagementVo> page;
        //BigDecimal amount = new BigDecimal("0");
        try {
            if (currentPage < 0 || currentPage.equals("")){
                currentPage = 1;
            }
            if (pageSize < 5 || currentPage.equals("")){
                pageSize = 5;
            }
            page = PageHelper.startPage(currentPage, pageSize);
            if (StringUtil.isNullOrEmpty(queryString) || queryString.equals("")){
                //billingManagementDao.selectPage(page,null);

                List<BillingManagementVo> list = billingManagementDao.selectBillingList();

                //计算金额
                BigDecimal totalAmount = list.stream().
                        filter(val -> val.getAmount() != null)
                        .map(val -> new BigDecimal(val.getAmount().toString()))
                        .reduce(BigDecimal.ZERO,BigDecimal::add);
                log.info("账单数据 ："+list);

                return new ResponseData(HttpStatus.SUCCESS,page.getTotal(),totalAmount.toString(), list);
            } else {
                //billingManagementDao.selectPage(page,null);
                //page = PageHelper.startPage(currentPage, pageSize);

                List<BillingManagementVo> list = billingManagementDao.selectBillingListQueryString("%"+ queryString +"%");

                //计算金额
                BigDecimal totalAmount = list.stream().
                        filter(val -> val.getAmount() != null)
                        .map(val -> new BigDecimal(val.getAmount().toString()))
                        .reduce(BigDecimal.ZERO,BigDecimal::add);
                log.info("账单数据 ：", totalAmount);

                return new ResponseData(HttpStatus.SUCCESS,page.getTotal(),totalAmount.toString(), list);
            }

        } catch (Exception e) {
            return new ResponseData(HttpStatus.ERROR, e.getMessage());
        }
    }

    @Override
    public ResponseData addBillingListData(BillingManagementVo billingManagementVo) {
        // Your existing code for adding billing data...
        billingManagementVo.setCreateTime(new Date());

        try {
            billingManagementDao.insertBillingListData(billingManagementVo);

            log.info("Inserted billing record with UUID: {}", billingManagementVo.getCreateTime());

            log.info(billingManagementVo.getUuid().toString());
            log.info("===========>"+billingManagementVo.getCreateTime());
            return new ResponseData(HttpStatus.SUCCESS, "录入成功");
        } catch (Exception e) {
             //return new ResponseData(HttpStatus.ERROR, e.getMessage());
            return new ResponseData(HttpStatus.ERROR, "数据不合法");
        }
    }
}
